<template >
  <div>
    <el-dialog
      :title="add.isAdd ? '添加商品规格' : '修改商品规格'"
      :visible.sync="add.isShow"
      center
      :before-close="cancel"
    >
      <el-form :model="form" :rules="rules" ref="form1">
        <el-form-item
          label="规格名称"
          :label-width="formLabelWidth"
          prop="specsname"
        >
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item
          label="规格属性"
          :label-width="formLabelWidth"
          v-for="(item, index) in attrArr"
          :key="item.id"
        >
          <el-input
            class="inp_Info"
            v-model="item.value"
            autocomplete="off"
          ></el-input>

          <el-button v-if="index == 0" @click="add_info" type="primary"
            >新增规格属性</el-button
          >
          <el-button v-else @click="del_info(index)" type="danger"
            >删除</el-button
          >
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { specsinfo, specsedit, specsadd } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["add"],
  data() {
    return {
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        specsname: "", //商品规格名称
        attrs: "", //商品规格属性值
        status: 1, //状态1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        specsname: [
          {
            required: true,
            message: "请输入商品规格名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
      //定义一个数组 用于循环遍历动态表单项
      attrArr: [
        {
          value: "", //input的双向数据绑定内容
        },
      ],
    };
  },

  computed: {
    ...mapGetters({
      getspect: "specs/getspect", // 获取总数据
      getcount: "specs/getcount", //获取总页数
      getsize: "specs/getsize", // 查询条数
    }),
  },

  mounted() {},

  methods: {
    ...mapActions({
      getSpectAction: "specs/getSpectAction", // 获取总数据
      getCountAction: "specs/getCountAction", // 获取总页数
      changePageAction: "specs/changePageAction", // 重新触发列表的事件
    }),

    // 关闭表单自动 关闭的操作，   将数据传回 父元素
    cancel() {
      this.$emit("cancel", false);

      this.reset();
    },

    // 点击 对话框例的添加
    add_info() {
      // 添加判断
      if (this.attrArr.length > 3) {
        this.$message.warning("最多添加3项");
      } else {
        this.attrArr.push({
          value: "",
        });
      }
    },

    // 点击对话框 里的 删除
    del_info(index) {
      this.attrArr.splice(0, index);
    },

    //清除表单
    reset() {
      this.form = {
        specsname: "", //商品规格名称
        attrs: "", //规格属性值
        status: 1, //状态1正常2禁用
      };

      this.$refs.form1.resetFields();

      //清空动态表单项
      this.attrArr = [
        {
          value: "",
        },
      ];
    },

    //封装一个查询一条数据的接口
    lookup(id) {
      specsinfo({
        id,
      }).then((res) => {
        console.log(res);
        if (res.code == 200) {
          // 将 res.list[0]  赋值给 this。form
          this.form = res.list[0];
          this.form.id = id;

          this.form.attrs.map((item, idx) => {
            if (idx == 0) {
              this.attrArr[0].value = item;
            } else {
              this.attrArr.push({
                value: item,
              });
            }
          });
        }
      });
    },

    // 点击确认    isAdd == true  是添加，   false 是 修改
    confirm() {
      this.$refs.form1.validate((valid) => {
        if (valid) {
          /* 
						    后端要求attrs的数据格式是："1,2,3"
						    我们获取的动态表单项的数组是 [{value:1},{value:2},{value:3}]
						    总结： [{value:1},{value:2},{value:3}]  =>"1,2,3"
						    步骤：[{value:1},{value:2},{value:3}] =>[1,2,3]=>"1,2,3"
						  */
          this.form.attrs = this.attrArr.map((item) => item.value).join(",");
          if (this.add.isAdd) {
            specsadd(this.form).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                //重新调用接口列表以及总条数
                this.getSpectAction();
                this.getCountAction();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            specsedit(this.form).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                //重新调用接口列表以及总条数
                this.getSpectAction();
                this.getCountAction();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.inp_Info {
  width: 67%;
}
</style>